import sys
from com.l2emu.gameserver.model.quest        import State
from com.l2emu.gameserver.model.quest        import QuestState
from com.l2emu.gameserver.model.quest.jython import QuestJython as JQuest

qn = "5025_Evolve"

#Minimum pet level in order to evolve
WOLF_MIN_LEVEL = 55
GREATW_MIN_LEVEL = 70
BABY_MIN_LEVEL = 55
STRIDER_MIN_LEVEL = 55

#Maximum distance allowed between pet and owner
MAX_DISTANCE = 100

#Messages
default = "<html><body>You are either not on a quest that involves this NPC, or you don't meet this NPC's minimum quest requirements.</body></html>"
error_1 = "<html><body><br>For the evolution of a pet, there are 2 requirements:<br>1. The pet must satisfy the required level.<br>2. The pet must be summoned.</body></html>"
error_2 = "Your pet cannot be evolved."
error_3 = "Your pet should be nearby."
error_lvl_wolf = "<html><body><br>Your wolf needs to be level "+str(WOLF_MIN_LEVEL)+" in order for it to evolve.</body></html>"
error_lvl_greatw = "<html><body><br>Your great wolf needs to be level "+str(GREATW_MIN_LEVEL)+" in order for it to evolve.</body></html>"
error_lvl_fenrir = "<html><body><br>Your fenrir needs to be level "+str(GREATW_MIN_LEVEL)+" in order for it to evolve.</body></html>"
error_lvl_baby = "<html><body><br>Your baby pet needs to be level "+str(BABY_MIN_LEVEL)+" in order for it to evolve.</body></html>"
error_lvl_strider = "<html><body><br>Your strider needs to be level "+str(STRIDER_MIN_LEVEL)+" in order for it to evolve.</body></html>"
end_msg_wolf = "<html><body><br>Great job, your Wolf"
end_msg2_wolf= "has become a Great Wolf, enjoy!</body></html>"
end_msg_gwolf = "<html><body><br>Great job, your Great Wolf"
end_msg2_gwolf = "has become a Fenrir, enjoy!</body></html>"
end_msg3_gwolf = "has become a Great White Wolf, enjoy!</body></html>"
end_msg_baby = "<html><body><br>Great job, your "
end_msg2_baby = "has become a Improved "
end_msg3_baby = " enjoy!!!</body></html>"
end_msg_strider = "<html><body><br>Great job, your "
end_msg2_strider = "has become a Red "
end_msg3_strider = " enjoy!!!</body></html>"
end_msg_fenrir = "<html><body><br>Great job, your Fenrir"
end_msg2_fenrir = "has become a White Fenrir, enjoy!</body></html>"

NPCSCH = [35440,35442,35444,35446,35448,35450,35567,35569,35571,35573,35575,35577,35579]

#Items
WOLF_COLLAR = 2375
GREAT_WOLF_NECK = 9882
FENRIR_NECK = 10426
GREAT_SNOW_WOLF_NECK = 10307
SNOW_FENRIR_NECK = 10611
BABY_BUFFALO_PAN = 6648
BABY_KOOK_OCARINA = 6650
BABY_COUGAR_CHIME = 6649
IMPROVED_BUFFALO_PAN = 10311
IMPROVED_KOOK_OCARINA = 10313
IMPROVED_COUGAR_CHIME = 10312
WSTRIDER_BUGLE = 4422
SSTRIDER_BUGLE = 4423
TSTRIDER_BUGLE = 4424
RED_WSTRIDER_BUGLE = 10308
RED_SSTRIDER_BUGLE = 10309
RED_TSTRIDER_BUGLE = 10310

#Pets
WOLF = 12077
GREATW = 16025
FENRIR = 16041
SFENRIR = 16042
GREATSW = 16037
BABYS = [12780,12781,12782]
WSTRIDER = 12526
SSTRIDER = 12527
TSTRIDER = 12528

UPDATE_CONTROL_ITEMS = { 
			WOLF_COLLAR:GREAT_WOLF_NECK,
			GREAT_WOLF_NECK:FENRIR_NECK,
			BABY_BUFFALO_PAN:IMPROVED_BUFFALO_PAN,
			BABY_KOOK_OCARINA:IMPROVED_KOOK_OCARINA,
			BABY_COUGAR_CHIME:IMPROVED_COUGAR_CHIME,
			GREAT_SNOW_WOLF_NECK:SNOW_FENRIR_NECK
			}

EXCHANGE_CONTROL_ITEMS = {
			GREAT_WOLF_NECK:GREAT_SNOW_WOLF_NECK,
			FENRIR_NECK:SNOW_FENRIR_NECK,
			WSTRIDER_BUGLE:RED_WSTRIDER_BUGLE,
			SSTRIDER_BUGLE:RED_SSTRIDER_BUGLE,
			TSTRIDER_BUGLE:RED_TSTRIDER_BUGLE
			}

def get_control_item(st) :
  item = st.getPlayer().getPet().getControlItemId()
  if st.getState() == State.CREATED :
    st.set("item",str(item))
  else :
    if st.getInt("item") != item :
      item = 0
  return item  

def get_distance(player) :
  is_far = False
  if abs(player.getPet().getX() - player.getX()) > MAX_DISTANCE :
    is_far = True
  if abs(player.getPet().getY() - player.getY()) > MAX_DISTANCE :
    is_far = True
  if abs(player.getPet().getZ() - player.getZ()) > MAX_DISTANCE :
    is_far = True
  return is_far

class Quest (JQuest) :

 def __init__(self, id, name, descr): 
   JQuest.__init__(self, id, name, descr)

 def onEvent(self, event, st) :
   player = st.getPlayer()
   if event == "wolf" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [WOLF] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < WOLF_MIN_LEVEL :
       htmltext = error_lvl_wolf
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_wolf+name+end_msg2_wolf
       item = UPDATE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "greatw" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [GREATW] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < GREATW_MIN_LEVEL :
       htmltext = error_lvl_greatw
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_gwolf+name+end_msg2_gwolf
       item = UPDATE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "baby" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in BABYS :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < BABY_MIN_LEVEL :
       htmltext = error_lvl_baby
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_baby+player.getPet().getTemplate().getName()+name+end_msg2_baby+player.getPet().getTemplate().getName()+end_msg3_baby
       item = UPDATE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "greatsw" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [GREATW] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < WOLF_MIN_LEVEL :
       htmltext = error_lvl_wolf
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_gwolf+name+end_msg3_gwolf
       item = EXCHANGE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "fenrir" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [FENRIR] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < GREATW_MIN_LEVEL :
       htmltext = error_lvl_fenrir
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_fenrir+name+end_msg2_fenrir
       item = EXCHANGE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "wstrider" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [WSTRIDER] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < STRIDER_MIN_LEVEL :
       htmltext = error_lvl_strider
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_strider+player.getPet().getTemplate().getName()+name+end_msg2_strider+player.getPet().getTemplate().getName()+end_msg3_strider
       item = EXCHANGE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "sstrider" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [SSTRIDER] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < STRIDER_MIN_LEVEL :
       htmltext = error_lvl_strider
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_strider+player.getPet().getTemplate().getName()+name+end_msg2_strider+player.getPet().getTemplate().getName()+end_msg3_strider
       item = EXCHANGE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")
   elif event == "tstrider" :
     if player.getPet() == None :
       htmltext = error_1
       st.exitQuest(1)
     elif player.getPet().getTemplate().npcId not in [TSTRIDER] :
       htmltext = error_2
       st.exitQuest(1)
     elif player.getPet().getLevel() < STRIDER_MIN_LEVEL :
       htmltext = error_lvl_strider
       st.exitQuest(1)
     elif get_distance(player) :
       htmltext = error_3
       st.exitQuest(1)
     elif get_control_item(st) == 0 :
       htmltext = error_2
       st.exitQuest(1)
     else :
       name = player.getPet().getName()
       if name == None : name = " "
       else : name = " "+name+" "
       htmltext = end_msg_strider+player.getPet().getTemplate().getName()+name+end_msg2_strider+player.getPet().getTemplate().getName()+end_msg3_strider
       item = EXCHANGE_CONTROL_ITEMS[player.getInventory().getItemByObjectId(player.getPet().getControlItemId()).getItemId()]
       player.getPet().giveAllToOwner()
       player.getPet().deleteMe(player) #both despawn pet and delete controlitem
       st.giveItems(item, 1)
       st.exitQuest(1)
       st.playSound("ItemSound.quest_finish")                                 
   return htmltext

 def onTalk(self, npc, player):
   htmltext = default
   st = player.getQuestState(qn)
   if not st : return htmltext
   htmltext = "Chamberlain.htm"
   return htmltext

QUEST       = Quest(-1, qn, "Custom")

for i in NPCSCH:
   QUEST.addStartNpc(i)
   QUEST.addTalkId(i)